package AccountWindow.HotelAdmin.AccountDetailWindow;

import AccountWindow.HotelAdmin.AccountDetailWindow.Dao.AccountDetailDao;
import AccountWindow.SystemAdmin.Account;
import AccountWindow.SystemAdmin.NewRegisterWindow.Dao.NewRegisterDAO;
import SignAndRegister.RemindWindow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

public class AccountDetailWindow extends JPanel {

    private static Vector<Vector> data;
    private JTable jTable;
    private JScrollPane jScrollPane;
    private JPanel buttonPanel;
    private JPanel scrollPanel;
    private JPanel nowAccountPanel;
    private JTextField idField;
    private JComboBox<String> nowAccountTypeBox;
    private JTextField nameField;
    private JTextField phoneField;
    private JTextField codeField;
    private JButton searchButton;
    public JPanel accountDetailPanel;

    public AccountDetailWindow(String nowAccountID){

        try {

            accountDetailPanel = new JPanel(new BorderLayout());
            this.add(accountDetailPanel);

            nowAccountPanel = new JPanel(null);
            nowAccountPanel.setPreferredSize(new Dimension(400,100));
            createNowAccounPanel();

            jTable = getTable();
            jScrollPane = new JScrollPane(jTable);
            scrollPanel = new JPanel(new GridLayout(0,1));
            scrollPanel.add(jScrollPane);

            buttonPanel = new JPanel(new FlowLayout());
            searchButton = new JButton("查询");
//            addButton = new JButton("添加");
//            modifyButton = new JButton("修改");

            searchButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String id = idField.getText();
                    try {
                        Account searchAccount = new AccountDetailDao().queryByID(id);
                        if(searchAccount!=null){
                            nowAccountTypeBox.setSelectedItem(searchAccount.accountType);
                            nameField.setText(searchAccount.accountName);
                            phoneField.setText(searchAccount.accountPhone);
                            codeField.setText(searchAccount.accountCode);
                        } else {
                            new RemindWindow("NoThisAccount");
                        }
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });



            buttonPanel.add(searchButton);

            nowAccountPanel.setPreferredSize(new Dimension(600,100));
            scrollPanel.setPreferredSize(new Dimension(600,320));
            buttonPanel.setPreferredSize(new Dimension(600,100));

            accountDetailPanel.add(nowAccountPanel,BorderLayout.NORTH);
            accountDetailPanel.add(scrollPanel,BorderLayout.CENTER);
            accountDetailPanel.add(buttonPanel,BorderLayout.SOUTH);

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void createNowAccounPanel(){

        JLabel idLabel = new JLabel("用户ID:");
        idField = new JTextField(10);
        nowAccountPanel.add(idLabel);
        nowAccountPanel.add(idField);

        JLabel typeLabel = new JLabel("用户类型:");
        JPanel nowAccountTypePanel = new JPanel(null);
        nowAccountTypeBox = new JComboBox<String>();
        nowAccountTypeBox.addItem("");
        nowAccountTypeBox.addItem("住客");
        nowAccountTypeBox.addItem("职工");
        nowAccountTypeBox.addItem("酒店管理员");
        nowAccountTypeBox.addItem("系统管理员");
        nowAccountTypePanel.add(nowAccountTypeBox);
        nowAccountTypeBox.setBounds(0,0,200,20);
        nowAccountPanel.add(typeLabel);
        nowAccountPanel.add(nowAccountTypePanel);

        JLabel nameLabel = new JLabel("用户名:");
        nameField = new JTextField(10);
        nowAccountPanel.add(nameLabel);
        nowAccountPanel.add(nameField);

        JLabel codeLabel = new JLabel("用户密码:");
        codeField = new JTextField(10);
        nowAccountPanel.add(codeLabel);
        nowAccountPanel.add(codeField);

        JLabel phoneLabel = new JLabel("联系电话:");
        phoneField = new JTextField(10);
        nowAccountPanel.add(phoneLabel);
        nowAccountPanel.add(phoneField);

        JLabel onlyLookLabel = new JLabel("仅查看:");
        JPanel typeChoicePanel = new JPanel(null);
        JComboBox<String> typeBox = new JComboBox<String>();
        typeBox.addItem("任何类型");
        typeBox.addItem("住客");
        typeBox.addItem("职工");
        typeBox.addItem("酒店管理员");
        typeBox.addItem("系统管理员");
        typeChoicePanel.add(typeBox);
        typeBox.setBounds(0,0,200,20);
        typeBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedType = (String) typeBox.getSelectedItem();
                try{
                    if(selectedType.equals("任何类型")){
                        refreshAccountDetailPanel();
                    }else{
                        filterTableByType(selectedType);
                        refreshTale();
                    }
                }catch (Exception error){
                    error.printStackTrace();
                }
            }
        });
        nowAccountPanel.add(onlyLookLabel);
        nowAccountPanel.add(typeChoicePanel);

        idLabel.setBounds(0,5,100,20);
        idField.setBounds(50,5,200,20);
        typeLabel.setBounds(280,5,100,20);
        nowAccountTypePanel.setBounds(340,5,200,20);
        nameLabel.setBounds(0,35,100,20);
        nameField.setBounds(50,35,200,20);
        codeLabel.setBounds(280,35,100,20);
        codeField.setBounds(340,35,200,20);
        onlyLookLabel.setBounds(0,65,100,20);
        typeChoicePanel.setBounds(50,65,200,20);
        phoneLabel.setBounds(280,65,100,20);
        phoneField.setBounds(340,65,200,20);

    }

    private void refreshTale() throws Exception {
        if(noFirstFlag){
            scrollPanel.remove(jScrollPane);
        }
        noFirstFlag = true;
        jScrollPane = new JScrollPane(jTable);
        scrollPanel.add(jScrollPane,BorderLayout.CENTER);
        scrollPanel.updateUI();
        scrollPanel.invalidate();
        scrollPanel.validate();
        scrollPanel.repaint();
    }

    private void filterTableByType(String accountType) throws Exception {
        data.clear();
        data = new AccountDetailDao().queryByType(accountType);
        Vector headers = new Vector<>();
        headers.add("用户ID");
        headers.add("用户类型");
        headers.add("用户名");
        headers.add("用户联系电话");
        headers.add("用户密码");
        jTable = new JTable(data,headers);
    }

    private boolean noFirstFlag = false;

    private JTable getTable() throws Exception {
        Vector headers = new Vector<>();
        headers.add("用户ID");
        headers.add("用户类型");
        headers.add("用户名");
        headers.add("用户联系电话");
        headers.add("用户密码");

        if (noFirstFlag) {
            data.clear();   //如果不是第一次进入，则将data内所有数据清空

        }
        noFirstFlag = true;
        data = new AccountDetailDao().query();

        JTable jTable = new JTable(data, headers);
        jTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int beSelectedRow = jTable.getSelectedRow();
                idField.setText(jTable.getValueAt(beSelectedRow,0).toString());
                nowAccountTypeBox.setSelectedItem(jTable.getValueAt(beSelectedRow,1).toString());
                nameField.setText(jTable.getValueAt(beSelectedRow,2).toString());
                phoneField.setText(jTable.getValueAt(beSelectedRow,3).toString());
                codeField.setText(jTable.getValueAt(beSelectedRow,4).toString());
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });


        return jTable;
    }

    public void refreshAccountDetailPanel() throws Exception {

        if(noFirstFlag){
            scrollPanel.remove(jScrollPane);
        }
        noFirstFlag = true;
        jTable = getTable();
        jScrollPane = new JScrollPane(jTable);
        scrollPanel.add(jScrollPane,BorderLayout.CENTER);
        scrollPanel.updateUI();
        scrollPanel.invalidate();
        scrollPanel.validate();
        scrollPanel.repaint();
    }

//-------------TableCellEditorButtonOfDelete专场--------------------------------------------------------------------------

    private class TableCellEditorButtonOfDelete extends DefaultCellEditor {
        //该类为值编辑器，当用户对表格内的组件有动作的时候，有该类对事件做出反应
        private JButton jButton;
        public TableCellEditorButtonOfDelete() {
            super(new JTextField());    //使用父类的构造方法
            this.setClickCountToStart(1);   //编辑点击一次即可激活操作
            this.jButton = new JButton("删除");
            jButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("系统管理员删除了该名用户");
                }
            });
        }

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){     //在这里定义被点击后发送的事件
            String deleteAccountID = (String) table.getValueAt(row,0);
            String deleteAccountType = (String) table.getValueAt(row,1);
            String deleteAccountCode = (String) table.getValueAt(row,3);
            Account deleteAccount = new Account(deleteAccountID,deleteAccountType,deleteAccountCode);
            try {
                int isDeleteSuccess = new AccountDetailDao().remove(deleteAccount);
                if(isDeleteSuccess==1){
                    new NewRegisterDAO().remove(deleteAccount); //在该类中应注意删表的同时要在其他如customer表、employee表等中同步删除,在本例中customer尤其注意
                }else{
                    new RemindWindow("DeleteFailure");
                }
                refreshAccountDetailPanel();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return jButton;
        }
    }

}
